package com.example.myapplication.base;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.AnimRes;
import androidx.annotation.AnimatorRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 过场动画。
 */
public class UITransitionAnimation implements Parcelable {

    /**
     * 无动画。
     */
    public static final UITransitionAnimation TRANSIT_NONE =
            new UITransitionAnimation(FragmentTransaction.TRANSIT_NONE);
    /**
     * 打开动画。
     */
    public static final UITransitionAnimation TRANSIT_FRAGMENT_OPEN =
            new UITransitionAnimation(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    /**
     * 关闭动画。
     */
    public static final UITransitionAnimation TRANSIT_FRAGMENT_CLOSE =
            new UITransitionAnimation(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    /**
     * 渐变动画。
     */
    public static final UITransitionAnimation TRANSIT_FRAGMENT_FADE =
            new UITransitionAnimation(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    @AnimatorRes
    @AnimRes
    private final int mEnterAnim;
    @AnimatorRes
    @AnimRes
    private final int mExitAnim;
    @Transit
    private final int mTransition;

    /**
     * 创建一个新的过场动画。
     *
     * @param enter 进场动画ID。
     * @param exit  退场动画ID。
     */
    @SuppressLint("WrongConstant")
    public UITransitionAnimation(@AnimatorRes @AnimRes int enter, @AnimatorRes @AnimRes int exit) {
        mEnterAnim = enter;
        mExitAnim = exit;
        mTransition = FragmentTransaction.TRANSIT_UNSET;
    }

    private UITransitionAnimation(@Transit int transition) {
        mEnterAnim = 0;
        mExitAnim = 0;
        mTransition = transition;
    }

    /**
     * Parcel构造器。
     *
     * @param source Parcel源。
     */
    protected UITransitionAnimation(@NonNull Parcel source) {
        mEnterAnim = source.readInt();
        mExitAnim = source.readInt();
        mTransition = source.readInt();
    }

    /**
     * 将参数填充到{@link FragmentTransaction}对象中。
     *
     * @param fragmentTransaction {@link FragmentTransaction}对象。
     */
    public void fillFragmentTransaction(@NonNull FragmentTransaction fragmentTransaction) {
        ArgUtils.checkNonNull("fragmentTransaction", fragmentTransaction);
        if (mEnterAnim != 0 || mExitAnim != 0) {
            fragmentTransaction.setCustomAnimations(mEnterAnim, mExitAnim);
        } else if (mTransition != FragmentTransaction.TRANSIT_UNSET) {
            fragmentTransaction.setTransition(mTransition);
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mEnterAnim);
        dest.writeInt(mExitAnim);
        dest.writeInt(mTransition);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * Parcel创建器。
     */
    public static final Creator<UITransitionAnimation> CREATOR = new Creator<UITransitionAnimation>() {
        @Override
        public UITransitionAnimation createFromParcel(Parcel source) {
            return new UITransitionAnimation(source);
        }

        @Override
        public UITransitionAnimation[] newArray(int size) {
            return new UITransitionAnimation[size];
        }
    };

    @IntDef({FragmentTransaction.TRANSIT_NONE, FragmentTransaction.TRANSIT_FRAGMENT_OPEN,
            FragmentTransaction.TRANSIT_FRAGMENT_CLOSE, FragmentTransaction.TRANSIT_FRAGMENT_FADE})
    @Retention(RetentionPolicy.SOURCE)
    private @interface Transit {
    }

}
